import { app, ipcMain, Tray, Menu, shell, dialog } from 'electron'
import DB from './db'
import path from 'path'
import pkg from '../../package.json'

let tray = null

export function getDataPath() {
  return app.getPath('userData')
}

ipcMain.handle('getDataPath', () => {
  return getDataPath()
})

export function initExtra() {
  const storePath = getDataPath()
  DB.initDB(storePath)
}

export function createTray(setPosition) {
  tray = new Tray(path.join(__static, './tary.png'))
  const contextMenu = Menu.buildFromTemplate([
    {
      label: '关于',
      // role: 'about'
      click() {
        dialog.showMessageBox({
          title: pkg.name,
          message: pkg.description,
          detail: `Version: ${pkg.version}\nAuthor: ${pkg.author}\nGithub: http://www.baidu.com`
        })
      }
    },
    {
      label: '问题反馈',
      click: () => {
        shell.openExternal('http://www.baidu.com')
      }
    },
    {
      label: '退出',
      role: 'quit'
    }
  ])
  tray.setContextMenu(contextMenu)
  tray.setToolTip(pkg.name)
  tray.on('click', (event, bounds, position) => {
    setPosition()
  })
}
